package br.com.jhora.main.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JHoraFilter implements Filter {


	@Override
	public void destroy() {
		System.out.println("Executando destroy...");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain filterChain) throws IOException, ServletException {

		final HttpServletRequest baseRequest = (HttpServletRequest) req;
		final HttpServletResponse baseResponse = (HttpServletResponse) res;
		
		System.out.println(baseRequest.getRequestURL());
		if(baseRequest.getRequestURL().toString().contains("admin")){
		baseRequest.setAttribute("selectedScreen",
				 baseRequest.getServletPath());
		      RequestDispatcher dispatcher =
		    		  baseRequest.getRequestDispatcher("/jsp/JHoraAdmin.jsp");
		      if (dispatcher != null){		    	  
		    	  dispatcher.forward(baseRequest, baseResponse);
		      }
		}	
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("Executando init...!!!!!!!!!");
	}

}
